

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">

  <link rel="apple-touch-icon" sizes="76x76" href="/img/goose.png">
  <link rel="icon" href="/img/goose.png">
  

  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Victor">
  <meta name="keywords" content="">
  
    <meta name="description" content="3.Packages and Builtin Functions   dir(): This function takes in an object and returns the dir() of that object giving us the attributes of the object. 123456789101112131415&gt;&gt;&gt; name &#x3D; &#x27;R">
<meta property="og:type" content="article">
<meta property="og:title" content="notes_The Python Book_20240418">
<meta property="og:url" content="https://zhoucourier.github.io/2024/04/03/notes_The%20Python%20Book_20240418/index.html">
<meta property="og:site_name" content="Victor&#39;s blog">
<meta property="og:description" content="3.Packages and Builtin Functions   dir(): This function takes in an object and returns the dir() of that object giving us the attributes of the object. 123456789101112131415&gt;&gt;&gt; name &#x3D; &#x27;R">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-04-02T17:16:58.440Z">
<meta property="article:modified_time" content="2024-11-30T13:19:30.226Z">
<meta property="article:author" content="Victor">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>notes_The Python Book_20240418 - Victor&#39;s blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/KaTeX/0.16.2/katex.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1749284_5i9bdhy70f8.css">



<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1736178_k526ubmyhba.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"zhoucourier.github.io","root":"/","version":"1.9.8","typing":{"enable":true,"typeSpeed":50,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"leancloud":{"app_id":"tug7FNRscgxNLfy5wuyQCOHw-MdYXbMMI","app_key":"3wRjVGCsJDHKYTxHzELIibA7","server_url":null,"path":"window.location.pathname","ignore_local":true},"umami":{"src":null,"website_id":null,"domains":null,"start_time":"2024-01-01T00:00:00.000Z","token":null,"api_server":null}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  

  

  

  

  

  
    
  



  
<meta name="generator" content="Hexo 7.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Victor</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/" target="_self">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/languages/" target="_self">
                <i class="iconfont icon-language-fill"></i>
                <span>languages</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="notes_The Python Book_20240418"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2024-04-03 01:16" pubdate>
          2024年4月3日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          3.7k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          32 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span> 次
        </span>
        
      
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">notes_The Python Book_20240418</h1>
            
              <p id="updated-time" class="note note-info" style="">
                
                  
                    本文最后更新于 2024年11月30日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h2 id="3-Packages-and-Builtin-Functions">3.Packages and Builtin Functions</h2>
<ul>
<li>
<p>dir(): This function takes in an object and returns the <em>dir</em>() of that object giving us the attributes of the object.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>name = <span class="hljs-string">&#x27;Rob&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">dir</span>(name)<br>[<span class="hljs-string">&#x27;__add__&#x27;</span>, <span class="hljs-string">&#x27;__class__&#x27;</span>, <span class="hljs-string">&#x27;__contains__&#x27;</span>, <span class="hljs-string">&#x27;__delattr__&#x27;</span>, <span class="hljs-string">&#x27;__dir__&#x27;</span>, <span class="hljs-string">&#x27;__doc__&#x27;</span>,<br><span class="hljs-string">&#x27;__eq__&#x27;</span>, <span class="hljs-string">&#x27;__format__&#x27;</span>, <span class="hljs-string">&#x27;__ge__&#x27;</span>, <span class="hljs-string">&#x27;__getattribute__&#x27;</span>, <span class="hljs-string">&#x27;__getitem__&#x27;</span>,<br><span class="hljs-string">&#x27;__getnewargs__&#x27;</span>, <span class="hljs-string">&#x27;__gt__&#x27;</span>, <span class="hljs-string">&#x27;__hash__&#x27;</span>, <span class="hljs-string">&#x27;__init__&#x27;</span>, <span class="hljs-string">&#x27;__init_subclass__&#x27;</span>,<br><span class="hljs-string">&#x27;__iter__&#x27;</span>, <span class="hljs-string">&#x27;__le__&#x27;</span>, <span class="hljs-string">&#x27;__len__&#x27;</span>, <span class="hljs-string">&#x27;__lt__&#x27;</span>, <span class="hljs-string">&#x27;__mod__&#x27;</span>, <span class="hljs-string">&#x27;__mul__&#x27;</span>, <span class="hljs-string">&#x27;__ne__&#x27;</span>,<br><span class="hljs-string">&#x27;__new__&#x27;</span>, <span class="hljs-string">&#x27;__reduce__&#x27;</span>, <span class="hljs-string">&#x27;__reduce_ex__&#x27;</span>, <span class="hljs-string">&#x27;__repr__&#x27;</span>, <span class="hljs-string">&#x27;__rmod__&#x27;</span>,<br><span class="hljs-string">&#x27;__rmul__&#x27;</span>, <span class="hljs-string">&#x27;__setattr__&#x27;</span>, <span class="hljs-string">&#x27;__sizeof__&#x27;</span>, <span class="hljs-string">&#x27;__str__&#x27;</span>, <span class="hljs-string">&#x27;__subclasshook__&#x27;</span>,<br><span class="hljs-string">&#x27;capitalize&#x27;</span>, <span class="hljs-string">&#x27;casefold&#x27;</span>, <span class="hljs-string">&#x27;center&#x27;</span>, <span class="hljs-string">&#x27;count&#x27;</span>, <span class="hljs-string">&#x27;encode&#x27;</span>, <span class="hljs-string">&#x27;endswith&#x27;</span>,<br><span class="hljs-string">&#x27;expandtabs&#x27;</span>, <span class="hljs-string">&#x27;find&#x27;</span>, <span class="hljs-string">&#x27;format&#x27;</span>, <span class="hljs-string">&#x27;format_map&#x27;</span>, <span class="hljs-string">&#x27;index&#x27;</span>, <span class="hljs-string">&#x27;isalnum&#x27;</span>, <span class="hljs-string">&#x27;isalpha&#x27;</span>,<br><span class="hljs-string">&#x27;isascii&#x27;</span>, <span class="hljs-string">&#x27;isdecimal&#x27;</span>, <span class="hljs-string">&#x27;isdigit&#x27;</span>, <span class="hljs-string">&#x27;isidentifier&#x27;</span>, <span class="hljs-string">&#x27;islower&#x27;</span>, <span class="hljs-string">&#x27;isnumeric&#x27;</span>,<br><span class="hljs-string">&#x27;isprintable&#x27;</span>, <span class="hljs-string">&#x27;isspace&#x27;</span>, <span class="hljs-string">&#x27;istitle&#x27;</span>, <span class="hljs-string">&#x27;isupper&#x27;</span>, <span class="hljs-string">&#x27;join&#x27;</span>, <span class="hljs-string">&#x27;ljust&#x27;</span>, <span class="hljs-string">&#x27;lower&#x27;</span>,<br><span class="hljs-string">&#x27;lstrip&#x27;</span>, <span class="hljs-string">&#x27;maketrans&#x27;</span>, <span class="hljs-string">&#x27;partition&#x27;</span>, <span class="hljs-string">&#x27;replace&#x27;</span>, <span class="hljs-string">&#x27;rfind&#x27;</span>, <span class="hljs-string">&#x27;rindex&#x27;</span>, <span class="hljs-string">&#x27;rjust&#x27;</span>,<br><span class="hljs-string">&#x27;rpartition&#x27;</span>, <span class="hljs-string">&#x27;rsplit&#x27;</span>, <span class="hljs-string">&#x27;rstrip&#x27;</span>, <span class="hljs-string">&#x27;split&#x27;</span>, <span class="hljs-string">&#x27;splitlines&#x27;</span>, <span class="hljs-string">&#x27;startswith&#x27;</span>,<br><span class="hljs-string">&#x27;strip&#x27;</span>, <span class="hljs-string">&#x27;swapcase&#x27;</span>, <span class="hljs-string">&#x27;title&#x27;</span>, <span class="hljs-string">&#x27;translate&#x27;</span>, <span class="hljs-string">&#x27;upper&#x27;</span>, <span class="hljs-string">&#x27;zfill&#x27;</span>]<br></code></pre></td></tr></table></figure>
<p>This code demonstrates that in Python, when you use the <code>dir()</code> function on a string object, it returns a list of all the attributes and methods of that object. <code>dir()</code> is a very useful built-in function for exploring an object’s capabilities, or in other words, to see what attributes and methods are available for use on that object.<br>
In the example you provided, <code>name</code> is a string object with the value ‘Rob’. When you call <code>dir(name)</code>, you get a list that contains all the methods of the string object, such as <code>upper()</code> for converting the string to uppercase, <code>lower()</code> for converting the string to lowercase, and <code>split()</code> for splitting the string into substrings, among others.<br>
Most of these methods are standard for operating on strings, while those with double underscores before and after their names, such as <code>__add__</code> and <code>__len__</code>, are Python’s magic methods (also known as special methods). These methods typically serve special functions; for example, the <code>__add__</code> method implements the addition operator <code>+</code>, allowing you to concatenate two strings using <code>+</code>.<br>
This list is very useful for understanding what can be done with Python strings, and it also shows the dynamic nature and flexibility of the Python language. With these methods, you can easily manipulate and work with text data.</p>
</li>
</ul>
<h2 id="4-Data-Types">4.Data Types</h2>
<ul>
<li>
<pre><code class="language-python">&lt;class &quot;str&quot;&gt;
&lt;class &quot;int&quot;&gt;
&lt;class &quot;float&quot;&gt;
+
-
*
/
<figure class="highlight gml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs gml">  <br>- <span class="hljs-built_in">real</span> <span class="hljs-keyword">and</span> imaginary parts of complex numbers<br><br>  ~~~python<br>  &gt;&gt;&gt; <span class="hljs-variable language_">x</span>.<span class="hljs-built_in">real</span><br>  &gt;&gt;&gt; <span class="hljs-variable language_">x</span>.imag<br></code></pre></td></tr></table></figure>

</code></pre>
</li>
<li>
<p>Integers or floats can be converted into a boolean using the built-in function bool. This treats any value as 0 or 0.0 as False and any other value to be True.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x<br><span class="hljs-literal">True</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>y = <span class="hljs-built_in">bool</span>(<span class="hljs-number">0.0</span>) <br>y<br><span class="hljs-literal">False</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>z = <span class="hljs-built_in">bool</span>(-<span class="hljs-number">10</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>z<br><span class="hljs-literal">True</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>Surprisingly we can use the operators in this chapter on boolean variables. The key to note is that a value of True is evaluated as 1 and False as 0, so you can see examples of this below.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-literal">True</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>y = <span class="hljs-literal">False</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x + y<br><span class="hljs-number">1</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x - y<br><span class="hljs-number">1</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x * y<br><span class="hljs-number">0</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x / y<br>Traceback (most recent call last): File <span class="hljs-string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;<br>ZeroDivisionError: division by zero &gt;&gt;&gt; x = <span class="hljs-literal">True</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>y = <span class="hljs-literal">True</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x + y<br><span class="hljs-number">2</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x - y<br><span class="hljs-number">0</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x + y<br><span class="hljs-number">2</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x * y<br><span class="hljs-number">1</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x / y<br><span class="hljs-number">1.0</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-literal">False</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>y = <span class="hljs-literal">False</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x + y<br><span class="hljs-number">0</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x - y<br><span class="hljs-number">0</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x * y<br><span class="hljs-number">0</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x / y<br>Traceback (most recent call last):<br>File <span class="hljs-string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt; ZeroDivisionError: division by zero<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>create byte, byte arrays and memory view objects</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-string">b&quot;Hello World&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br><span class="hljs-string">b&#x27;Hello World&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>y = <span class="hljs-built_in">bytearray</span>(<span class="hljs-number">6</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>y<br><span class="hljs-built_in">bytearray</span>(<span class="hljs-string">b&#x27;\x00\x00\x00\x00\x00\x00&#x27;</span>) <br><span class="hljs-meta">&gt;&gt;&gt; </span>z = <span class="hljs-built_in">memoryview</span>(<span class="hljs-built_in">bytes</span>(<span class="hljs-number">5</span>))<br><span class="hljs-meta">&gt;&gt;&gt; </span>z<br>&lt;memory at <span class="hljs-number">0x7fdde6fea348</span>&gt;<br></code></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="5-Operators">5.Operators</h2>
<ul>
<li>
<p>define a variable, assigning the variable</p>
</li>
<li>
<p>It is a very subtle difference so you need to be careful with it. A simpler explanation is that == returns True if the variables being compared are equal, whereas is checks whether they are the same.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs python"><br><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-number">1</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>a <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <br><span class="hljs-literal">True</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>a == <span class="hljs-number">1</span> <br><span class="hljs-literal">True</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>a = [] <br><span class="hljs-meta">&gt;&gt;&gt; </span>b = [] <br><span class="hljs-meta">&gt;&gt;&gt; </span>a <span class="hljs-keyword">is</span> b <br><span class="hljs-literal">False</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>a == b <br><span class="hljs-literal">True</span><br></code></pre></td></tr></table></figure>
<p>However, they are both lists so using the comparison statement == we return True as they are both empty lists. If we assigned a as a list and b = a we would get the following:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = [] <br><span class="hljs-meta">&gt;&gt;&gt; </span>b = a <br><span class="hljs-meta">&gt;&gt;&gt; </span>a <span class="hljs-keyword">is</span> b<br><span class="hljs-literal">True</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<pre><code class="language-python">x/y #division
x//y #floor division
x%y #modulus
x**y #exponentiation
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs routeros"><br><br><br><span class="hljs-comment">## 6.**Dates**</span><br><br>- datetime()<br><br>  ~~~python<br>  &gt;&gt;&gt; <span class="hljs-keyword">from</span> datetime import daretime timedelta date<br>  &gt;&gt;&gt; d1 = datetime(2017, 5, 16, 10, 0, 11)<br>  &gt;&gt;&gt; change = timedelta(<span class="hljs-attribute">days</span>=1, <span class="hljs-attribute">hours</span>=2, <span class="hljs-attribute">minutes</span>=10)<br>  &gt;&gt;&gt; d2 = date.today()<br>  &gt;&gt;&gt; d3 = datetime.datetime.now()<br></code></pre></td></tr></table></figure>

</code></pre>
</li>
</ul>
<h2 id="7-Lists">7.Lists</h2>
<ul>
<li>
<p>pop</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>stuff.pop() <span class="hljs-comment">#removes the last item of the list</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>append</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>stuff.append(<span class="hljs-number">9</span>) <span class="hljs-comment">#add an element to the end of the list</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>remove</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>stuff.remove(<span class="hljs-number">9</span>) <span class="hljs-comment"># by using the attribute remove,remove 9 from the list,specify the name of the item we wanted to remove</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>count</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>count_list = [<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">9</span>,<span class="hljs-number">9</span>,<span class="hljs-number">9</span>] <span class="hljs-comment">#get the count of in the list</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>count_list.count(<span class="hljs-number">1</span>)<br><span class="hljs-number">3</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>count_list.count(<span class="hljs-number">4</span>)<br><span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>reverse</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>count_list.reverse() <span class="hljs-comment">#reverses the elements in a list</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>count_list<br>[<span class="hljs-number">9</span>, <span class="hljs-number">9</span>, <span class="hljs-number">9</span>, <span class="hljs-number">6</span>, <span class="hljs-number">5</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>sort</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>count_list.sort() <span class="hljs-comment">#we can only use the sort method on a list of numeric values</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>count_list<br>[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">9</span>, <span class="hljs-number">9</span>, <span class="hljs-number">9</span>]<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>len</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">len</span>(stuff) <span class="hljs-comment">#get the length of the list</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>used negative indexing to choose item in a list</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>stuff[-<span class="hljs-number">1</span>]<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>choose element</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>stuff[<span class="hljs-number">1</span>:<span class="hljs-number">3</span>] <span class="hljs-comment">#take from the element in index 1 in the list and show up to but not including element in index 3 in the list</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>stuff[<span class="hljs-number">1</span>:] <span class="hljs-comment">#select everything except the first element</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>stuff <span class="hljs-comment">#select everything,shows the full list</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>new_stuff = stuff[-<span class="hljs-number">1</span>:]<br><span class="hljs-meta">&gt;&gt;&gt; </span>new_stuff = stuff[:-<span class="hljs-number">1</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>stuff[<span class="hljs-number">1</span>:<span class="hljs-number">8</span>:<span class="hljs-number">2</span>]<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>boolean</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-number">9</span> <span class="hljs-keyword">in</span> stuff <span class="hljs-comment">#If the value is in we get back a boolean value True or False.</span><br><span class="hljs-literal">True</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>copy</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>new_stuff = stuff.copy() <span class="hljs-comment">#take a copy of the list</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>clear</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>stuff.clear() <span class="hljs-comment">#simply put this method clears the list of all its content</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>stuff<br>[]<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>select</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>,<span class="hljs-number">7</span>,<span class="hljs-number">2</span>)<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>obtain the start, stop and step of the range object alongside the count and index</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-built_in">range</span>(<span class="hljs-number">7</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x = x[<span class="hljs-number">1</span>:]<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br><span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>, <span class="hljs-number">7</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x.start<br><span class="hljs-number">1</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x.stop<br><span class="hljs-number">7</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x.step<br><span class="hljs-number">1</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x.index(<span class="hljs-number">1</span>)<br><span class="hljs-number">0</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x.count(<span class="hljs-number">1</span>)<br><span class="hljs-number">1</span><br></code></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="8-Tuples">8.<strong>Tuples</strong></h2>
<p>You access them in exactly the same way and many things we covered in lists are relevant to tuples, the big difference is tuples can’t be modified.</p>
<ul>
<li>
<pre><code class="language-python">&gt;&gt;&gt; numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
&gt;&gt;&gt; numbers
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
<figure class="highlight vim"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs vim"><br>- namely <span class="hljs-built_in">count</span><br><br>  ~~~<span class="hljs-keyword">python</span><br>  &gt;&gt;&gt; new_numbers = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">7</span>, <span class="hljs-number">9</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>)<br>  &gt;&gt;&gt; new_numbers.<span class="hljs-built_in">count</span>(<span class="hljs-number">2</span>)<br>  <span class="hljs-number">3</span><br></code></pre></td></tr></table></figure>

</code></pre>
</li>
<li>
<p>index</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>new_numbers = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-number">7</span>, <span class="hljs-number">9</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>new_numbers.index(<span class="hljs-number">2</span>)<br><span class="hljs-number">1</span><br></code></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="9-Dictionaries">9.<strong>Dictionaries</strong></h2>
<ul>
<li>
<p>definition</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details = &#123;&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details[<span class="hljs-string">&quot;first name&quot;</span>] = <span class="hljs-string">&quot;Rob&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details[<span class="hljs-string">&quot;surname&quot;</span>] = <span class="hljs-string">&quot;Mastrodomenico&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details[<span class="hljs-string">&quot;gender&quot;</span>] = <span class="hljs-string">&quot;Male&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details[<span class="hljs-string">&quot;favourite_food&quot;</span>] = <span class="hljs-string">&quot;Pizza&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details<br>&#123;<span class="hljs-string">&quot;first name&quot;</span>: <span class="hljs-string">&quot;Rob&quot;</span>, <span class="hljs-string">&quot;favourite_food&quot;</span>: <span class="hljs-string">&quot;Pizza&quot;</span>, <span class="hljs-string">&quot;surname&quot;</span>: <span class="hljs-string">&quot;Mastrodomenico&quot;</span>, <span class="hljs-string">&quot;gender&quot;</span>: <span class="hljs-string">&quot;Male&quot;</span>&#125;<br></code></pre></td></tr></table></figure>
<p>or</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>person_details = <span class="hljs-built_in">dict</span>(first_name=<span class="hljs-string">&quot;Rob&quot;</span>, surname=<span class="hljs-string">&quot;Mastrodomenico&quot;</span>,gender=<span class="hljs-string">&quot;Male&quot;</span>, favourite_food=<span class="hljs-string">&quot;Pizza&quot;</span>)<br></code></pre></td></tr></table></figure>
<p>or</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details = &#123;<span class="hljs-string">&quot;first name&quot;</span>: <span class="hljs-string">&quot;Rob&quot;</span>, <span class="hljs-string">&quot;surname&quot;</span>: <span class="hljs-string">&quot;Mastrodomenico&quot;</span>,<span class="hljs-string">&quot;gender&quot;</span>: <span class="hljs-string">&quot;Male&quot;</span>, <span class="hljs-string">&quot;favourite_food&quot;</span>: <span class="hljs-string">&quot;Pizza&quot;</span>&#125;<br></code></pre></td></tr></table></figure>
<p>or</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details = <span class="hljs-built_in">dict</span>([(<span class="hljs-string">&quot;first name&quot;</span>, <span class="hljs-string">&quot;Rob&quot;</span>), (<span class="hljs-string">&quot;surname&quot;</span>, <span class="hljs-string">&quot;Mastrodomenico&quot;</span>), (<span class="hljs-string">&quot;gender&quot;</span>, <span class="hljs-string">&quot;Male&quot;</span>), (<span class="hljs-string">&quot;favourite_food&quot;</span>, <span class="hljs-string">&quot;Pizza&quot;</span>)])<br></code></pre></td></tr></table></figure>
<p>or</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment">#using the fromkeys method.</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = (<span class="hljs-string">&#x27;key1&#x27;</span>, <span class="hljs-string">&#x27;key2&#x27;</span>, <span class="hljs-string">&#x27;key3&#x27;</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>y = <span class="hljs-number">0</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>res = <span class="hljs-built_in">dict</span>.fromkeys(x, y)<br><span class="hljs-meta">&gt;&gt;&gt; </span>res<br>&#123;<span class="hljs-string">&#x27;key1&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;key2&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;key3&#x27;</span>: <span class="hljs-number">0</span>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>deal with it in a more sophisticated way: use a try except statement.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details = <span class="hljs-built_in">dict</span>([(<span class="hljs-string">&quot;first name&quot;</span>, <span class="hljs-string">&quot;Rob&quot;</span>), (<span class="hljs-string">&quot;surname&quot;</span>, <span class="hljs-string">&quot;Mastrodomenico&quot;</span>),<br>(<span class="hljs-string">&quot;gender&quot;</span>, <span class="hljs-string">&quot;Male&quot;</span>), (<span class="hljs-string">&quot;favourite_food&quot;</span>, <span class="hljs-string">&quot;Pizza&quot;</span>)])<br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details<br>&#123;<span class="hljs-string">&quot;first name&quot;</span>: <span class="hljs-string">&quot;Rob&quot;</span>, <span class="hljs-string">&quot;surname&quot;</span>: <span class="hljs-string">&quot;Mastrodomenico&quot;</span>, <span class="hljs-string">&quot;favourite_food&quot;</span>: <span class="hljs-string">&quot;Pizza&quot;</span>,<br><span class="hljs-string">&quot;gender&quot;</span>: <span class="hljs-string">&quot;Male&quot;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">try</span>:<br><span class="hljs-meta">... </span>		age = personal_details[<span class="hljs-string">&quot;age&quot;</span>]<br><span class="hljs-meta">... </span><span class="hljs-keyword">except</span> KeyError:<br><span class="hljs-meta">... </span>		age = <span class="hljs-literal">None</span><br>...<br><span class="hljs-meta">&gt;&gt;&gt; </span>age<br>&gt;&gt;&gt;<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>pop</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details<br>&#123;<span class="hljs-string">&#x27;first name&#x27;</span>: <span class="hljs-string">&#x27;Rob&#x27;</span>, <span class="hljs-string">&#x27;surname&#x27;</span>: <span class="hljs-string">&#x27;Mastrodomenico&#x27;</span>, <span class="hljs-string">&#x27;gender&#x27;</span>: <span class="hljs-string">&#x27;Male&#x27;</span>, <span class="hljs-string">&#x27;favourite_food&#x27;</span>:<br> <span class="hljs-string">&#x27;Pizza&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details.pop(<span class="hljs-string">&#x27;gender&#x27;</span>)<br><span class="hljs-string">&#x27;Male&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details<br>&#123;<span class="hljs-string">&#x27;first name&#x27;</span>: <span class="hljs-string">&#x27;Rob&#x27;</span>, <span class="hljs-string">&#x27;surname&#x27;</span>: <span class="hljs-string">&#x27;Mastrodomenico&#x27;</span>, <span class="hljs-string">&#x27;favourite_food&#x27;</span>: <span class="hljs-string">&#x27;Pizza&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details.popitem()<br>(<span class="hljs-string">&#x27;favourite_food&#x27;</span>, <span class="hljs-string">&#x27;Pizza&#x27;</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details<br>&#123;<span class="hljs-string">&#x27;first name&#x27;</span>: <span class="hljs-string">&#x27;Rob&#x27;</span>, <span class="hljs-string">&#x27;surname&#x27;</span>: <span class="hljs-string">&#x27;Mastrodomenico&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>del: we pass the dictionary name and key combination to remove that key value pair from the dictionary</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">del</span> personal_details[<span class="hljs-string">&#x27;gender&#x27;</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details<br>&#123;<span class="hljs-string">&#x27;first name&#x27;</span>: <span class="hljs-string">&#x27;Rob&#x27;</span>, <span class="hljs-string">&#x27;surname&#x27;</span>: <span class="hljs-string">&#x27;Mastrodomenico&#x27;</span>, <span class="hljs-string">&#x27;favourite_food&#x27;</span>: <span class="hljs-string">&#x27;Pizza&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>Earlier we mentioned how if we assign one list to another the changes are reflected. The same is true for dictionaries.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>his_details = personal_details<br></code></pre></td></tr></table></figure>
<p>If we want to take a copy of a dictionary and independently make changes to it we can use the copy method in a similar way that we did with lists.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>his_details = personal_details.copy()<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>clear out all contents of dictionary using the clear method</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details.clear()<br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details<br>&#123;&#125;<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>access all keys and values from a <code>dictionary</code> using the following methods</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details.items()<br>dict_items([(<span class="hljs-string">&#x27;first name&#x27;</span>, <span class="hljs-string">&#x27;Rob&#x27;</span>), (<span class="hljs-string">&#x27;surname&#x27;</span>, <span class="hljs-string">&#x27;Mastrodomenico&#x27;</span>), (<span class="hljs-string">&#x27;gender&#x27;</span>, <span class="hljs-string">&#x27;Male&#x27;</span>),<br> (<span class="hljs-string">&#x27;favourite_food&#x27;</span>, <span class="hljs-string">&#x27;Pizza&#x27;</span>)])<br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details.keys()<br>dict_keys([<span class="hljs-string">&#x27;first name&#x27;</span>, <span class="hljs-string">&#x27;surname&#x27;</span>, <span class="hljs-string">&#x27;gender&#x27;</span>, <span class="hljs-string">&#x27;favourite_food&#x27;</span>])<br><span class="hljs-meta">&gt;&gt;&gt; </span>personal_details.values()<br>dict_values([<span class="hljs-string">&#x27;Rob&#x27;</span>, <span class="hljs-string">&#x27;Mastrodomenico&#x27;</span>, <span class="hljs-string">&#x27;Male&#x27;</span>, <span class="hljs-string">&#x27;Pizza&#x27;</span>])<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>The objects that we return can be iterated over and this is covered later when we introduce loops. However if you want to access them like we would a <strong>list</strong> we can cast them as such and access the relevant index positions.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">list</span>(personal_details.items())[<span class="hljs-number">0</span>]<br>(<span class="hljs-string">&#x27;first name&#x27;</span>, <span class="hljs-string">&#x27;Rob&#x27;</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">list</span>(personal_details.keys())[-<span class="hljs-number">1</span>]<br><span class="hljs-string">&#x27;favourite_food&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">list</span>(personal_details.values())[:-<span class="hljs-number">1</span>]<br>[<span class="hljs-string">&#x27;Rob&#x27;</span>, <span class="hljs-string">&#x27;Mastrodomenico&#x27;</span>, <span class="hljs-string">&#x27;Male&#x27;</span>]<br></code></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="10-Sets">10.<strong>Sets</strong></h2>
<p>They are also <em><strong>unordered</strong></em> and cannot be changed.</p>
<p>Here, we can see that <em><strong>the ordering of the set doesn’t resemble</strong></em> what we put into it.</p>
<ul>
<li>
<p>create</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment">#use the curly brackets</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>names = &#123;<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Peter&#x27;</span>,<span class="hljs-string">&#x27;Natasha&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names<br>&#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
<p>or</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment">#use the set builtin function</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>names = <span class="hljs-built_in">set</span>((<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Peter&#x27;</span>,<span class="hljs-string">&#x27;Natasha&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>))<br><span class="hljs-meta">&gt;&gt;&gt; </span>names<br>&#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names = <span class="hljs-built_in">set</span>([<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Peter&#x27;</span>,<span class="hljs-string">&#x27;Natasha&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])<br><span class="hljs-meta">&gt;&gt;&gt; </span>names<br>&#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
<p>or</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment">#pass in a string using the curly brackets you retain the full string in but when passed in using set the string is split into the individual characters. Again note when the characters are split there is no ordering to them.</span><br><br><span class="hljs-meta">&gt;&gt;&gt; </span>names = &#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names<br>&#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names = <span class="hljs-built_in">set</span>(<span class="hljs-string">&#x27;Wanda&#x27;</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>names<br>&#123;<span class="hljs-string">&#x27;n&#x27;</span>, <span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;W&#x27;</span>, <span class="hljs-string">&#x27;d&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>notice: add <code>dic</code>,<code> list</code>,<code> tuple</code> and <code>set</code> to the <code>set</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_set = &#123;<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>my_set<br>&#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>, (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)&#125;<br></code></pre></td></tr></table></figure>
<p>The reason we can include the <strong>tuple</strong> over the <code>dictionary</code>,<code> list</code> and <code>set</code> is that the tuple cannot be changed so is supported in a set.</p>
</li>
<li>
<p>We can see if the value is in the <code>set</code> by using the following syntax:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&#x27;Tony&#x27;</span> <span class="hljs-keyword">in</span> names <span class="hljs-literal">True</span><br><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&#x27;Steve&#x27;</span> <span class="hljs-keyword">in</span> names <span class="hljs-literal">False</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>add</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>names.add(<span class="hljs-string">&#x27;Steve&#x27;</span>)<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>That aspect of <strong>not having duplicate values</strong> within the set is useful if we want to have a unique representation of values where we could have duplicates. For example, you could imagine a long <code>list</code> with lots of repeated values and you just want the unique values within it as we show below.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>days = [<span class="hljs-string">&#x27;Monday&#x27;</span>, <span class="hljs-string">&#x27;Monday&#x27;</span>, <span class="hljs-string">&#x27;Tuesday&#x27;</span>, <span class="hljs-string">&#x27;Wednesday&#x27;</span>,<br>            <span class="hljs-string">&#x27;Sunday&#x27;</span>, <span class="hljs-string">&#x27;Sunday&#x27;</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>days_set = <span class="hljs-built_in">set</span>(days)<br><span class="hljs-meta">&gt;&gt;&gt; </span>days_set<br>&#123;<span class="hljs-string">&#x27;Monday&#x27;</span>, <span class="hljs-string">&#x27;Wednesday&#x27;</span>, <span class="hljs-string">&#x27;Sunday&#x27;</span>, <span class="hljs-string">&#x27;Tuesday&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>operate on multiple sets, obtain the unique value between two sets</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment">#use the | operator</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>names = &#123;<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Peter&#x27;</span>,<span class="hljs-string">&#x27;Natasha&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names<br>&#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>more_names = &#123;<span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>more_names<br>&#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Carol&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names | more_names<br>&#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;<br><br><span class="hljs-comment">#use the set method union</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>names.union(more_names)<br>&#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
<p>achieve the same result as the list is converted into a set</p>
</li>
<li>
<p>Now where we used union and the | operator if we want to find out what values are in all sets we use the <code>intersection</code> method or the <code>&amp;</code> operator.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>names = &#123;<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Peter&#x27;</span>,<span class="hljs-string">&#x27;Natasha&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>more_names = &#123;<span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names &amp; more_names<br>&#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names.intersection(more_names)<br>&#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
<p>we can add non-sets into the intersection method</p>
</li>
<li>
<p>look at the differences between two or more sets, then we can use the <code>difference</code> method or the <code>−</code> operator</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>names - more_names<br>&#123;<span class="hljs-string">&#x27;Natasha&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names.difference(more_names)<br>&#123;<span class="hljs-string">&#x27;Natasha&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
<p>The manner in which difference is applied for more than one comparison is to <strong>work left to right</strong> so we first look at the difference between names and more_names and then look at the difference between this result and even_more_names.</p>
</li>
<li>
<p>use the symmetric_difference method or the <code>^</code> operator</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>names ^ more_names<br>&#123;<span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names.symmetric_difference(more_names)<br>&#123;<span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
<p>return back the elements that are <strong>in either set but not in both</strong>, so its like the or method but doesn’t include any common values.</p>
</li>
<li>
<p>isdisjoint method</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>names.isdisjoint(more_names)<br><span class="hljs-literal">False</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>more_names = &#123;<span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Bruce&#x27;</span>, <span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Sue&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names.isdisjoint(more_names)<br><span class="hljs-literal">True</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>issubset</p>
</li>
<li>
<p>issuperset</p>
</li>
<li>
<p>pop</p>
</li>
<li>
<p>remove</p>
</li>
<li>
<p>discard</p>
</li>
<li>
<p>clear</p>
</li>
<li>
<p>update</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>names = &#123;<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Peter&#x27;</span>,<span class="hljs-string">&#x27;Natasha&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names<br>&#123;<span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>more_names = &#123;<span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>more_names<br>&#123;<span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names | more_names<br>&#123;<span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names<br>&#123;<span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>more_names<br>&#123;<span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;<br><span class="hljs-meta">&gt;&gt;&gt; </span>names.update(more_names)<br><span class="hljs-meta">&gt;&gt;&gt; </span>names<br>&#123;<span class="hljs-string">&#x27;Carol&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Steve&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Tony&#x27;</span>&#125;<br></code></pre></td></tr></table></figure>
<p>The big difference here is that when you use the <code>|</code> operator you don’t change either of the sets, however using the <code>update</code> method changes the set that you have used the method for so in this case the <code>names</code> set is now the result of <code>names</code> <code>|</code> <code>more_names</code>.</p>
</li>
<li>
<p>frozen set</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>frozen_names = <span class="hljs-built_in">frozenset</span>(&#123;<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Peter&#x27;</span>,<span class="hljs-string">&#x27;Natasha&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>&#125;)<br><span class="hljs-meta">&gt;&gt;&gt; </span>frozen_names<br><span class="hljs-built_in">frozenset</span>(&#123;<span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;)<br><span class="hljs-meta">&gt;&gt;&gt; </span>frozen_names = <span class="hljs-built_in">frozenset</span>([<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Peter&#x27;</span>,<span class="hljs-string">&#x27;Natasha&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>])<br><span class="hljs-meta">&gt;&gt;&gt; </span>frozen_names<br><span class="hljs-built_in">frozenset</span>(&#123;<span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;)<br><span class="hljs-meta">&gt;&gt;&gt; </span>frozen_names = <span class="hljs-built_in">frozenset</span>((<span class="hljs-string">&#x27;Tony&#x27;</span>,<span class="hljs-string">&#x27;Peter&#x27;</span>,<span class="hljs-string">&#x27;Natasha&#x27;</span>,<span class="hljs-string">&#x27;Wanda&#x27;</span>))<br><span class="hljs-meta">&gt;&gt;&gt; </span>frozen_names<br><span class="hljs-built_in">frozenset</span>(&#123;<span class="hljs-string">&#x27;Tony&#x27;</span>, <span class="hljs-string">&#x27;Peter&#x27;</span>, <span class="hljs-string">&#x27;Wanda&#x27;</span>, <span class="hljs-string">&#x27;Natasha&#x27;</span>&#125;)<br><span class="hljs-meta">&gt;&gt;&gt; </span>frozen_names = <span class="hljs-built_in">frozenset</span>(<span class="hljs-string">&#x27;Tony&#x27;</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>frozen_names<br><span class="hljs-built_in">frozenset</span>(&#123;<span class="hljs-string">&#x27;T&#x27;</span>, <span class="hljs-string">&#x27;y&#x27;</span>, <span class="hljs-string">&#x27;n&#x27;</span>, <span class="hljs-string">&#x27;o&#x27;</span>&#125;)<br></code></pre></td></tr></table></figure>
<p>The frozen set is what the tuple is to a list in that it cannot be altered</p>
</li>
</ul>
<h2 id="11-Loops-if-Else-and-While">11.<strong>Loops, if, Else, and While</strong></h2>
<ul>
<li>
<p>if</p>
</li>
<li>
<p>else</p>
</li>
<li>
<p>elif</p>
</li>
<li>
<p>list</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">list</span>[<span class="hljs-number">0</span>][<span class="hljs-number">2</span>]<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>for in</p>
</li>
<li>
<p>while</p>
</li>
</ul>
<h2 id="12-Strings">12.<strong>Strings</strong></h2>
<ul>
<li>
<p>use an <code>escape sequence</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>single_quote_string = <span class="hljs-string">&#x27;string with a \&#x27; single quote&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>single_quote_string<br><span class="hljs-string">&quot;string with a &#x27; single quote&quot;</span><br></code></pre></td></tr></table></figure>
<p>We can get away with not using <code>escape sequences</code> if we use triple quotes. So we can rewrite the previous using <code>triple quotes</code> as follows:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>single_quote_string = <span class="hljs-string">&quot;&quot;&quot;string with a &#x27; single quote&quot;&quot;&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>single_quote_string<br><span class="hljs-string">&quot;string with a &#x27; single quote&quot;</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>typing <code>backslash n</code> gives us a carriage return in our string. But what if we want forward <code>slash n</code> in our string, we can use what is called a <code>raw string</code>:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>raw_string = <span class="hljs-string">r&quot;This has a \n in it&quot;</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>raw_string<br><span class="hljs-string">&quot;This has a \\n in it&quot;</span><br></code></pre></td></tr></table></figure>
<p>or</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>not_raw_string = <span class="hljs-string">&quot;This has a \\n in it&quot;</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>not_raw_string<br><span class="hljs-string">&quot;This has a \\n in it&quot;</span><br></code></pre></td></tr></table></figure>
<p>In both examples when we show the content of the string it has an extra forward slash but when it is printed this disappears. And yes if you wanted an extra slash in there just use three slashes.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>not_raw_string = <span class="hljs-string">&quot;This has a \\\n in it&quot;</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>not_raw_string<br><span class="hljs-string">&quot;This has a \\\n in it&quot;</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>select element</p>
</li>
<li>
<p>boolean</p>
</li>
<li>
<p>put a variable into our string we need only define the position in the string using curly brackets and then using the format method with the arguments passed in they get assigned to the appropriate positions.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>first_name = <span class="hljs-string">&quot;Rob&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>last_name = <span class="hljs-string">&quot;Mastrodomenico&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>name = <span class="hljs-string">&quot;First name: &#123;&#125;, Last name: &#123;&#125;&quot;</span>.<span class="hljs-built_in">format</span>(first_name, last_name) &gt;&gt;&gt; name<br><span class="hljs-string">&quot;First name: Rob, Last name: Mastrodomenico&quot;</span><br></code></pre></td></tr></table></figure>
<p>We can also give the positions in the curly brackets to where we want the variables assigned, so we could write the following:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>first_name = <span class="hljs-string">&quot;Rob&quot;</span><br> &gt;&gt;&gt; last_name = <span class="hljs-string">&quot;Mastrodomenico&quot;</span><br> &gt;&gt;&gt; name = <span class="hljs-string">&quot;First name: &#123;1&#125;, Last name: &#123;0&#125;&quot;</span>.<span class="hljs-built_in">format</span>(first_name, last_name) <br> &gt;&gt;&gt; name<br> &gt;&gt;&gt; <span class="hljs-string">&quot;First name: Mastrodomenico, Last name: Rob&quot;</span><br></code></pre></td></tr></table></figure>
<p>That is wrong but you get the point. We can also define each value as a variable and pass that variable name in the curly brackets.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>first_name = <span class="hljs-string">&quot;Rob&quot;</span><br> &gt;&gt;&gt; last_name = <span class="hljs-string">&quot;Mastrodomenico&quot;</span><br> &gt;&gt;&gt; name = <span class="hljs-string">&quot;First name: &#123;f&#125;, Last name: &#123;l&#125;&quot;</span>.<span class="hljs-built_in">format</span>(f=first_name, l=last_name)<br> &gt;&gt;&gt; name<br> <span class="hljs-string">&quot;First name: Rob, Last name: Mastrodomenico&quot;</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>convert a string to all uppercase letters and then all lowercase letters</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>name.lower()<br><span class="hljs-string">&quot;rob mastrodomenico&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>name.upper()<br><span class="hljs-string">&quot;ROB MASTRODOMENICO&quot;</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>split up strings</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>name = <span class="hljs-string">&quot;Rob Mastrodomenico&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>name.split(<span class="hljs-string">&quot; &quot;</span>)<br>[<span class="hljs-string">&quot;Rob&quot;</span>, <span class="hljs-string">&quot;Mastrodomenico&quot;</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>first_name, last_name = name.split(<span class="hljs-string">&quot; &quot;</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>first_name<br><span class="hljs-string">&quot;Rob&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>last_name<br><span class="hljs-string">&quot;Mastrodomenico&quot;</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>a comma separated string which you may find in a <code>csv file</code> can be split into the variables it contains</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>match_details = <span class="hljs-string">&quot;Manchester United,Arsenal,2,0&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>match_details<br><span class="hljs-string">&quot;Manchester United,Arsenal,2,0&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>match_details.split(<span class="hljs-string">&quot;,&quot;</span>)<br>[<span class="hljs-string">&quot;Manchester United&quot;</span>, <span class="hljs-string">&quot;Arsenal&quot;</span>, <span class="hljs-string">&quot;2&quot;</span>, <span class="hljs-string">&quot;0&quot;</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>home_team, away_team = match_details.split(<span class="hljs-string">&quot;,&quot;</span>)[<span class="hljs-number">0</span>:<span class="hljs-number">2</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>home_team<br><span class="hljs-string">&quot;Manchester United&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>away_team<br><span class="hljs-string">&quot;Arsenal&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>home_goals, away_goals = match_details.split(<span class="hljs-string">&quot;,&quot;</span>)[<span class="hljs-number">2</span>:<span class="hljs-number">4</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>home_goals<br><span class="hljs-string">&quot;2&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>away_goals<br><span class="hljs-string">&quot;0&quot;</span><br><br><span class="hljs-comment">#replace all the commas with colons</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>match_details = <span class="hljs-string">&quot;Manchester United,Arsenal,2,0&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>match_details<br><span class="hljs-string">&quot;Manchester United,Arsenal,2,0&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>match_details.replace(<span class="hljs-string">&quot;,&quot;</span>,<span class="hljs-string">&quot;:&quot;</span>)<br><span class="hljs-string">&quot;Manchester United:Arsenal:2:0&quot;</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>apply the <strong>join method</strong> on the string containing just the <code>comma</code> and it then creates <code>a string of the values</code> in the <code>list</code> separated by the string that we applied the method on.</p>
<p>This is a very useful method when it comes to <strong>creating strings from lists separated by a common value</strong>.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>details = [<span class="hljs-string">&#x27;Manchester United&#x27;</span>, <span class="hljs-string">&#x27;Arsenal&#x27;</span>, <span class="hljs-string">&#x27;2&#x27;</span>, <span class="hljs-string">&#x27;0&#x27;</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>match_details = <span class="hljs-string">&#x27;,&#x27;</span>.join(details)<br><span class="hljs-meta">&gt;&gt;&gt; </span>match_details<br><span class="hljs-string">&#x27;Manchester United,Arsenal,2,0&#x27;</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>len</p>
</li>
</ul>
<h2 id="13-Regular-Expressions">13.<strong>Regular Expressions</strong></h2>
<ul>
<li>
<p>package re</p>
</li>
<li>
<p>finding all the characters</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> re<br><span class="hljs-meta">&gt;&gt;&gt; </span>name = <span class="hljs-string">&#x27;Rob Mastrodomenico&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;[a-m]&quot;</span>, name)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;b&#x27;</span>, <span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;d&#x27;</span>, <span class="hljs-string">&#x27;m&#x27;</span>, <span class="hljs-string">&#x27;e&#x27;</span>, <span class="hljs-string">&#x27;i&#x27;</span>, <span class="hljs-string">&#x27;c&#x27;</span>]<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>find the integer values 0–9 within a sequence</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&#x27;Find all numerical values like 1, 2, 3&#x27;</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;\d&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;1&#x27;</span>, <span class="hljs-string">&#x27;2&#x27;</span>, <span class="hljs-string">&#x27;3&#x27;</span>]<br></code></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;[0-9]&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;1&#x27;</span>, <span class="hljs-string">&#x27;2&#x27;</span>, <span class="hljs-string">&#x27;3&#x27;</span>]<br></code></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&#x27;Find all numerical values like 1, 2, 3, 3&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;[0-9]&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;1&#x27;</span>, <span class="hljs-string">&#x27;2&#x27;</span>, <span class="hljs-string">&#x27;3&#x27;</span>, <span class="hljs-string">&#x27;3&#x27;</span>]<br></code></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;\d&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;1&#x27;</span>, <span class="hljs-string">&#x27;2&#x27;</span>, <span class="hljs-string">&#x27;3&#x27;</span>, <span class="hljs-string">&#x27;3&#x27;</span>]<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>look for specific patterns</p>
</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&quot;hello world&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;he..o&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;hello&#x27;</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&quot;hello helpo hesoo&quot;</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;he..o&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;hello&#x27;</span>, <span class="hljs-string">&#x27;helpo&#x27;</span>, <span class="hljs-string">&#x27;hesoo&#x27;</span>]<br></code></pre></td></tr></table></figure>
<ul>
<li>search specifically on the start of the string</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>txt =<span class="hljs-string">&#x27;starts at the end&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;^start&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;start&#x27;</span>]<br></code></pre></td></tr></table></figure>
<ul>
<li>look at the last word in the string by using ending the searched string with the $ sign</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&#x27;the last word is end&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;end$&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;end&#x27;</span>]<br></code></pre></td></tr></table></figure>
<ul>
<li>find the occurrences of ai followed by 0 or more x values</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&quot;The rain in Spain falls mainly in the plain!&quot;</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;aix*&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;ai&#x27;</span>, <span class="hljs-string">&#x27;ai&#x27;</span>, <span class="hljs-string">&#x27;ai&#x27;</span>, <span class="hljs-string">&#x27;ai&#x27;</span>]<br></code></pre></td></tr></table></figure>
<ul>
<li>Expanding on the previous example you can find the number of instances of the string ai followed by one or more x by adding the + symbol. Applying that to the same string as before gives us the result of an empty string as we don’t have aix within it.</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> re<br><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&quot;The rain in Spain falls mainly in the plain!&quot;</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;aix+&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[]<br></code></pre></td></tr></table></figure>
<ul>
<li>a specified number of characters, use curly brackets containing the number of instances we are interested in</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&#x27;The cow said moo&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;mo&#123;2&#125;&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;moo&#x27;</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;moo&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;moo&#x27;</span>]<br></code></pre></td></tr></table></figure>
<ul>
<li>use the | symbol between the two strings, to find one or another value</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&quot;The Avengers are earths mightiest heroes go Avengers&quot;</span> <br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;Avengers|heroes&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;Avengers&#x27;</span>, <span class="hljs-string">&#x27;heroes&#x27;</span>, <span class="hljs-string">&#x27;Avengers&#x27;</span>]<br></code></pre></td></tr></table></figure>
<ul>
<li>use special sequences (returns the whitespace)</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>txt = <span class="hljs-string">&quot;Is there whitespace&quot;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.findall(<span class="hljs-string">&quot;\s&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27; &#x27;</span>, <span class="hljs-string">&#x27; &#x27;</span>]<br></code></pre></td></tr></table></figure>
<ul>
<li>
<p>There are other special sequences that we can use, they are listed as follows:<br>
● \A: This matches if the characters defined are at the beginning of the string “\AIt”<br>
● \b: This matches if the characters defined are at the beginning or at the end of a word<br>
“\bain” r&quot;ain\b&quot;<br>
● \B Returns a match where the specified characters are present, but NOT at the beginning<br>
(or at the end) of a word (the “r” in the beginning is making sure that the string is being<br>
treated as a “raw string”) r&quot;\Bain&quot; r&quot;ain\B&quot;<br>
● \d Returns a match where the string contains digits (numbers from 0-9) “\d”<br>
● \D Returns a match where the string DOES NOT contain digits “\D”<br>
● \s Returns a match where the string contains a white space character “\s”<br>
● \S Returns a match where the string DOES NOT contain a white space character “\S”<br>
● \w Returns a match where the string contains any word characters (characters from a<br>
to Z, digits from 0-9, and the underscore _ character) “\w”<br>
● \W Returns a match where the string DOES NOT contain any word characters “\W”<br>
● \Z Returns a match if the specified characters are at the end of the string</p>
</li>
<li>
<p>split method</p>
</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.split(<span class="hljs-string">&quot;\s&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;The&#x27;</span>, <span class="hljs-string">&#x27;rain&#x27;</span>, <span class="hljs-string">&#x27;in&#x27;</span>, <span class="hljs-string">&#x27;Spain&#x27;</span>]<br></code></pre></td></tr></table></figure>
<ul>
<li>specify the number of times we want the split to be done by using the maxsplit argument</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.split(<span class="hljs-string">&quot;\s&quot;</span>, txt, maxsplit=<span class="hljs-number">1</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.split(<span class="hljs-string">&quot;\s&quot;</span>, txt, maxsplit=<span class="hljs-number">2</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;The&#x27;</span>, <span class="hljs-string">&#x27;rain&#x27;</span>, <span class="hljs-string">&#x27;in Spain&#x27;</span>]<br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.split(<span class="hljs-string">&quot;\s&quot;</span>, txt, maxsplit=<span class="hljs-number">3</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br>[<span class="hljs-string">&#x27;The&#x27;</span>, <span class="hljs-string">&#x27;rain&#x27;</span>, <span class="hljs-string">&#x27;in&#x27;</span>, <span class="hljs-string">&#x27;Spain&#x27;</span>]<br></code></pre></td></tr></table></figure>
<ul>
<li>replace on a string</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.sub(<span class="hljs-string">&quot;\s&quot;</span>, <span class="hljs-string">&quot;9&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br><span class="hljs-string">&#x27;The9rain9in9Spain&#x27;</span><br></code></pre></td></tr></table></figure>
<ul>
<li>combine</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.sub(<span class="hljs-string">&quot;\s&quot;</span>, <span class="hljs-string">&quot;9&quot;</span>, txt, <span class="hljs-number">1</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br><span class="hljs-string">&#x27;The9rain in Spain&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.sub(<span class="hljs-string">&quot;\s&quot;</span>, <span class="hljs-string">&quot;9&quot;</span>, txt, <span class="hljs-number">2</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br><span class="hljs-string">&#x27;The9rain9in Spain&#x27;</span><br><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.sub(<span class="hljs-string">&quot;\s&quot;</span>, <span class="hljs-string">&quot;9&quot;</span>, txt, <span class="hljs-number">3</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x<br><span class="hljs-string">&#x27;The9rain9in9Spain&#x27;</span><br></code></pre></td></tr></table></figure>
<ul>
<li>span</li>
</ul>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = re.search(<span class="hljs-string">&quot;ai&quot;</span>, txt)<br><span class="hljs-meta">&gt;&gt;&gt; </span>x.span()<br>(<span class="hljs-number">5</span>, <span class="hljs-number">7</span>)<br></code></pre></td></tr></table></figure>
<h2 id="14-Dealing-with-Files">14.Dealing with Files</h2>
<p>^</p>
<h2 id="15-Functions-and-Classes">15.<strong>Functions and Classes</strong></h2>
<ul>
<li>
<p>define a function called lottery: use the command def followed by the name that you want to give the function</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">lottery</span>(<span class="hljs-params"><span class="hljs-built_in">min</span>, <span class="hljs-built_in">max</span></span>):<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>Classes: can be very powerful objects which allow us to bundle together lots of functions and variables</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyClass</span>:<br><span class="hljs-meta">... </span>		x = <span class="hljs-number">10</span><br>...<br><span class="hljs-meta">&gt;&gt;&gt; </span>mc = MyClass()<br><span class="hljs-meta">&gt;&gt;&gt; </span>mc.x<br><span class="hljs-number">10</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>create an init method using <code>__init__</code> this initialiser</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">class</span> <span class="hljs-title class_">Lottery</span>:<br><span class="hljs-meta">... </span>   <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, <span class="hljs-built_in">min</span>=<span class="hljs-number">1</span>, <span class="hljs-built_in">max</span>=<span class="hljs-number">59</span>, draw_length=<span class="hljs-number">7</span></span>): <span class="hljs-variable language_">self</span>.<span class="hljs-built_in">min</span> = <span class="hljs-built_in">min</span><br><span class="hljs-meta">... </span>       <span class="hljs-variable language_">self</span>.<span class="hljs-built_in">max</span> = <span class="hljs-built_in">max</span><br><span class="hljs-meta">... </span>       <span class="hljs-variable language_">self</span>.draw_length = draw_length<br><span class="hljs-meta">... </span>   <span class="hljs-keyword">def</span> <span class="hljs-title function_">lottery</span>(<span class="hljs-params">self</span>):<br><span class="hljs-meta">... </span>       <span class="hljs-comment">#codes</span><br></code></pre></td></tr></table></figure>
</li>
<li>
<p>in this code, we define the class in the way we did before and call it Lottery. Next, we create an init method using <strong>init</strong> this initialiser is called when we define the class so we can pass in arguments from here that can be used within the class. Note that we can use the standard defaults but these are then assigned to the class by using the self-dot syntax which allows that value to be part of class and then allowed to be used anywhere within the class. We can create the class in the following example:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span>l = Lottery()<br><span class="hljs-meta">&gt;&gt;&gt; </span>l.lottery()<br><span class="hljs-meta">&gt;&gt;&gt; </span>l = Lottery(<span class="hljs-number">1</span>,<span class="hljs-number">49</span>,<span class="hljs-number">6</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>l.lottery()<br><span class="hljs-meta">&gt;&gt;&gt; </span>l = Lottery(draw_length=<span class="hljs-number">8</span>)<br><span class="hljs-meta">&gt;&gt;&gt; </span>l.lottery()<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>import the package sys and look at the sys.path list</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> sys<br><span class="hljs-meta">&gt;&gt;&gt; </span>sys.path<br>[<span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-string">&#x27;/Users/rob/anaconda3/lib/python37.zip&#x27;</span>,<br> <span class="hljs-string">&#x27;/Users/rob/anaconda3/lib/python3.7&#x27;</span>,<br> <span class="hljs-string">&#x27;/Users/rob/anaconda3/lib/python3.7/lib-dynload&#x27;</span>,<br><span class="hljs-string">&#x27;/Users/rob/anaconda3/lib/python3.7/site-packages&#x27;</span>,<br><span class="hljs-string">&#x27;/Users/rob/anaconda3/lib/python3.7/site-packages/aeosa&#x27;</span>]<br></code></pre></td></tr></table></figure>
<p>import the contents of the <a target="_blank" rel="noopener" href="http://lottery.py">lottery.py</a> file.</p>
<p>If we create a file called import_lottery.py in the same directory as the <a target="_blank" rel="noopener" href="http://lottery.py">lottery.py</a> we can run the Lottery class as follows:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> lottery <span class="hljs-keyword">import</span> *<br></code></pre></td></tr></table></figure>
</li>
</ul>
<link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>notes_The Python Book_20240418</div>
      <div>https://zhoucourier.github.io/2024/04/03/notes_The Python Book_20240418/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Victor</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2024年4月3日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-cc-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2024/09/05/%E5%B0%BE%E6%B3%A8/" title="关于LaTeX尾注">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">关于LaTeX尾注</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      

    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  


  
  




  <script>
  Fluid.utils.createScript('https://lib.baomitu.com/mermaid/8.14.0/mermaid.min.js', function() {
    mermaid.initialize({"theme":"default"});

    Fluid.utils.listenDOMLoaded(function() {
      Fluid.events.registerRefreshCallback(function() {
        if ('mermaid' in window) {
          mermaid.init();
        }
      });
    });
  });
</script>






    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量 
        <span id="leancloud-site-pv"></span>
         次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数 
        <span id="leancloud-site-uv"></span>
         人
      </span>
    
    

  

</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/5.0.0/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script defer src="/js/leancloud.js" ></script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
